package j.quartz.core.common.log;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class MangoPatternLayout extends PatternLayout {

	private String patternRegex;
	
	private static List<String> regexMap=new  ArrayList<String>();
	
	static{
		regexMap.add("(?<!\\d)(?:(?:1[34578]\\d{9}))(?!\\d)");  //手机号码
		regexMap.add("(?<!\\d)(?:(?:[0]{1}[0-9]{2,3}-[0-9]{7,8}))(?!\\d)");//电话号码 带 -
		regexMap.add("(?<!\\d)(?:(?:[0]{1}[0-9]{2,3}[0-9]{7,8}))(?!\\d)");//电话号码 不带 -
	}

	public String getPatternRegex() {
		return patternRegex;
	}

	public void setPatternRegex(String patternRegex) {
		this.patternRegex = patternRegex;
	}

	@Override
	public String doLayout(ILoggingEvent event) {
		String logMsg = super.doLayout(event);
		if(patternRegex==null || !"Y".equals(patternRegex.toUpperCase())){
			return logMsg;
		}
		
		for(String patternStr:regexMap){
			Pattern pattern = Pattern.compile(patternStr);
	        Matcher matcher = pattern.matcher(event.getFormattedMessage());
	        while (matcher.find()) {  
	            logMsg=matcher.replaceAll("【**敏感屏蔽**】");
	        } 
		}
		return logMsg;
	}
}